package cn.player.act;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.ArrayList;
import java.util.List;

import cn.player.R;
import cn.player.adapter.MainAdapter;
import io.reactivex.Flowable;

public class RxAndroidActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private ListView mLvRxAndroid;
    private MainAdapter mMainAdapter;
    private List<Integer> mTitleInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_android);

        findViews();
        setListeners();
        getTitleInfos();

        mMainAdapter = new MainAdapter(this, mTitleInfos, R.layout.main_item_view);
        mLvRxAndroid.setAdapter(mMainAdapter);
    }

    private void findViews() {
        mLvRxAndroid = (ListView) findViewById(R.id.lv_rx_android);
    }

    private void setListeners() {
        mLvRxAndroid.setOnItemClickListener(this);
    }

    private void getTitleInfos() {
        mTitleInfos = new ArrayList<>();
        mTitleInfos.add(R.string.rx_android_test1);
        mTitleInfos.add(R.string.rx_android_test2);
        mTitleInfos.add(R.string.rx_android_test3);
        mTitleInfos.add(R.string.rx_android_test4);
        mTitleInfos.add(R.string.rx_android_test5);
        mTitleInfos.add(R.string.rx_android_test6);
        mTitleInfos.add(R.string.rx_android_test7);
        mTitleInfos.add(R.string.rx_android_test8);
        mTitleInfos.add(R.string.rx_android_test9);
        mTitleInfos.add(R.string.rx_android_test10);
        mTitleInfos.add(R.string.rx_android_test11);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (mTitleInfos.get(position)) {
            case R.string.rx_android_test1:
//                backPressureTest();
                break;
            case R.string.rx_android_test2:
//                reactivePullTest();
                break;
            case R.string.rx_android_test3:
//                backPressureWithBuffer();
                break;
            case R.string.rx_android_test4:
//                backPressureBufferTest();
                break;
            case R.string.rx_android_test5:
//                backPressureDropTest();
                break;
            case R.string.rx_android_test6:
                //开始rx2.0
                break;
            case R.string.rx_android_test7:
                flowableRangeTest();
                break;
            case R.string.rx_android_test8:

                break;
            case R.string.rx_android_test9:

                break;
            case R.string.rx_android_test10:

                break;
            case R.string.rx_android_test11:

                break;
        }
    }

    /********************** start rxjava2.0 测试 start *********************************/
    private void flowableRangeTest(){
        Flowable.range(1,100)
                .subscribe(new Subscriber<Integer>() {
                    private Subscription mSubscription;
                    @Override
                    public void onSubscribe(Subscription s) {
                        mSubscription = s;
                        mSubscription.request(1);
                    }

                    @Override
                    public void onNext(Integer result) {
                        Log.i("zl", "onNext(),result = " + result);
                        mSubscription.request(1);
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.i("zl", "onError(),t = " + t);
                    }

                    @Override
                    public void onComplete() {
                        Log.i("zl", "onComplete()");
                    }
                });

    }
    /********************** end rxjava2.0 测试 end *********************************/

    /*//背压异常测试
    private void backPressureTest() {
        Observable.interval(1, TimeUnit.MILLISECONDS)
//                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.newThread())
//                .sample(200,TimeUnit.MILLISECONDS)
                .subscribe(new Subscriber<Long>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.i("zl", "onStart()");
//                        request(1);////interval操作符不支持背压策略,所以不会响应request方法,写了也无效果
                    }

                    @Override
                    public void onNext(Long result) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.i("zl", "onNext() result = " + result);
//                        request(1);

                    }

                    @Override
                    public void onCompleted() {
                        Log.i("zl", "onCompleted()");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("zl", "onError() e = " + e);
                        //抛异常了:e = rx.exceptions.MissingBackpressureException
                    }
                });
    }

    //响应式拉取测试,执行成功
    private void reactivePullTest() {
        Observable.range(1, 10000)
                .observeOn(Schedulers.newThread())
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.i("zl", "onStart()");
                        request(1);
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.i("zl", "onNext() integer = " + integer);
                        request(1);
                    }

                    @Override
                    public void onCompleted() {
                        Log.i("zl", "onCompleted()");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("zl", "onError() e = " + e);
                    }
                });


    }

    private void backPressureWithBuffer() {
        Observable.interval(1, TimeUnit.MILLISECONDS)
                .observeOn(Schedulers.newThread())
                .buffer(100, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<List<Long>>() {
                    @Override
                    public void call(List<Long> results) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.i("zl", "call() size = " + results.size());
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.i("zl", "onError() throwable = " + throwable);
                    }
                });
    }

    private void backPressureBufferTest() {
        Observable.interval(1, TimeUnit.MILLISECONDS)
                .onBackpressureBuffer()
                .observeOn(Schedulers.newThread())
                .subscribe(new Subscriber<Long>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.i("zl", "onStart()");
                    }

                    @Override
                    public void onNext(Long integer) {
                        Log.i("zl", "onNext() integer = " + integer);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        Log.i("zl", "onCompleted()");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("zl", "onError() e = " + e);
                    }
                });


    }

    private void backPressureDropTest() {
        Observable.interval(1, TimeUnit.MILLISECONDS)
                .onBackpressureDrop()
                .observeOn(Schedulers.newThread())
                .subscribe(new Subscriber<Long>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.i("zl", "onStart()");
                    }

                    @Override
                    public void onNext(Long integer) {
                        Log.i("zl", "onNext() integer = " + integer);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        Log.i("zl", "onCompleted()");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("zl", "onError() e = " + e);
                    }
                });

    }*/





}
